package com.jiyici.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import android.text.format.DateFormat;

import com.jiyici.R;
import com.jiyici.model.Record;

public class DateUtil {
    
    public static class SimpleDate {
        private int year;
        private int month;
        private int day;
        public SimpleDate(int year, int month, int day) {
            super();
            this.year = year;
            this.month = month;
            this.day = day;
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + day;
            result = prime * result + month;
            result = prime * result + year;
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            SimpleDate other = (SimpleDate) obj;
            if (day != other.day)
                return false;
            if (month != other.month)
                return false;
            if (year != other.year)
                return false;
            return true;
        }
        
    }
    
    public static HashMap<SimpleDate, Integer> calculateDayCount(ArrayList<Record> recordList) {
        int size = recordList.size();
        HashMap<SimpleDate, Integer> result = new HashMap<DateUtil.SimpleDate, Integer>();
        for (int i = 0; i < size; i++) {
            Calendar c = Calendar.getInstance();
            c.setTimeInMillis(recordList.get(i).recordTime);
            SimpleDate sd = new SimpleDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE));
            Integer count = result.get(sd);
            if (count == null)
                count = 0;
            result.put(sd, count + 1);
        }
        return result;
    }

    public static String getDateOrTime(int dateGroup, long dateValue) {
        if (dateGroup >= DateGroup.GROUP_THIS_WEEK_AFTER_TOMORROW
                && dateGroup <= DateGroup.GROUP_THIS_WEEK_BEFORE_YESTERDAY) {
            return stringTime(dateValue);
        }
        return stringDate(dateValue);
    }
    
    public static String getDateOrTime(long dateValue) {
        Calendar date = Calendar.getInstance();
        date.setTimeInMillis(dateValue);
        int dateGroup = DateGroup.getGroupOfDate(date, DateGroup.getToday());
        if (dateGroup >= DateGroup.GROUP_TOMORROW
                && dateGroup <= DateGroup.GROUP_YESTERDAY) {
            return getGroupName(dateGroup) + " " + stringTime(dateValue);
        }
        return stringDate(dateValue);
    }

    private static final String SFORMAT_WEEKDAY_LONG = "EEEE";

    public static String getDateWeekName(long dateValue) {
        return new SimpleDateFormat(SFORMAT_WEEKDAY_LONG, Locale.getDefault()).format(new Date(dateValue));
    }

    public static String stringTime(long dateValue) {
        return DateFormat.getTimeFormat(Global.APP_CONTEXT).format(new Date(dateValue));
    }

    public static String stringDate(long dateValue) {
        return DateFormat.getDateFormat(Global.APP_CONTEXT).format(new Date(dateValue));
    }

    public static String stringDateTime(long dateValue) {
        return stringDate(dateValue) + " " + stringTime(dateValue);
    }

    public static String getGroupName(int group) {
        int resId = 0;
        switch (group) {
        case DateGroup.GROUP_INVALID:
            resId = 0;
            break;
        case DateGroup.GROUP_BEYOND_NEXT_MONTH:
            resId = R.string.date_group_beyond_next_month;
            break;
        case DateGroup.GROUP_NEXT_MONTH:
            resId = R.string.date_group_next_month;
            break;
        case DateGroup.GROUP_LATER_THIS_MONTH:
            resId = R.string.date_group_later_this_month;
            break;
        case DateGroup.GROUP_3_WEEKS_AWAY:
            resId = R.string.date_group_three_weeks_away;
            break;
        case DateGroup.GROUP_2_WEEKS_AWAY:
            resId = R.string.date_group_two_weeks_away;
            break;
        case DateGroup.GROUP_NEXT_WEEK:
            resId = R.string.date_group_next_week;
            break;
        case DateGroup.GROUP_THIS_WEEK_AFTER_TOMORROW:
            resId = 0;
            break;
        case DateGroup.GROUP_TOMORROW:
            resId = R.string.date_group_tommorrow;
            break;
        case DateGroup.GROUP_TODAY:
            resId = R.string.date_group_today;
            break;
        case DateGroup.GROUP_YESTERDAY:
            resId = R.string.date_group_yesterday;
            break;
        case DateGroup.GROUP_THIS_WEEK_BEFORE_YESTERDAY:
            resId = 0;
            break;
        case DateGroup.GROUP_LAST_WEEK:
            resId = R.string.date_group_last_week;
            break;
        case DateGroup.GROUP_2_WEEKS_AGO:
            resId = R.string.date_group_two_weeks_ago;
            break;
        case DateGroup.GROUP_3_WEEKS_AGO:
            resId = R.string.date_group_three_weeks_ago;
            break;
        case DateGroup.GROUP_EARLIER_THIS_MONTH:
            resId = R.string.date_group_earlier_this_month;
            break;
        case DateGroup.GROUP_LAST_MONTH:
            resId = R.string.date_group_last_month;
            break;
        case DateGroup.GROUP_OLDER:
            resId = R.string.date_group_older;
            break;
        }

        if (resId != 0) {
            return Global.APP_CONTEXT.getString(resId);
        }
        return "";
    }

}
